package org.example.bombermanclient.network;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.MessageToMessageDecoder;
import org.example.bombermanclient.model.Message;
import org.example.bombermanclient.util.JsonUtils;

import java.nio.charset.StandardCharsets;
import java.util.List;

public class JsonMessageDecoder extends MessageToMessageDecoder<ByteBuf> {

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
        String json = msg.toString(StandardCharsets.UTF_8).trim();
        System.out.println("DecodeMessage: " + json);
        Message message = JsonUtils.fromJson(json, Message.class);
        out.add(message);
    }
}
